//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================



using System;
using System.Configuration;

namespace GuidanceExplorer.Config
{
    public class ServerCollection : ConfigurationElementCollection
    {
        public ServerCollection()
        {
            ServerElement server = (ServerElement)CreateNewElement();
            // Add the element to the collection.
            Add(server);
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ServerElement();
        }

        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((ServerElement)element).Url;
        }


        public new string AddElementName
        {
            get
            {
                return base.AddElementName;
            }

            set
            {
                base.AddElementName = value;
            }

        }

        public new string ClearElementName
        {
            get
            {
                return base.ClearElementName;
            }

            set
            {
                base.AddElementName = value;
            }
        }


        public new string RemoveElementName
        {
            get
            {
                return base.RemoveElementName;
            }
        }

        public new int Count
        {

            get
            {
                return base.Count;
            }
        }


        public ServerElement this[int index]
        {
            get
            {
                return (ServerElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        new public ServerElement this[string url]
        {
            get
            {
                return (ServerElement)BaseGet(url);
            }
        }

        public int IndexOf(ServerElement server)
        {
            return BaseIndexOf(server);
        }

        public void Add(ServerElement server)
        {
            BaseAdd(server);
        }

        protected override void BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
        }

        public void Remove(ServerElement server)
        {
            if (BaseIndexOf(server) >= 0)
                BaseRemove(server.Url);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string url)
        {
            BaseRemove(url);
        }

        public void Clear()
        {
            BaseClear();
        }
    }

    public class ServerElement : ConfigurationElement
    {
        private string _url;
        private string _userName;
        private string _passWord;

        [ConfigurationProperty("url", IsRequired = false)]
        public string Url
        {
            get
            {
                return (string)this["url"];
            }
            set
            {
                this["url"] = value;
            }
        }

        [ConfigurationProperty("username", IsRequired = false)]
        public string UserName
        {
            get
            {
                return (string)this["username"];
            }
            set
            {
                this["username"] = value;
            }
        }

        [ConfigurationProperty("password", IsRequired = false)]
        public string Password
        {
            get
            {
                return (string)this["password"];
            }
            set
            {
                this["password"] = value;
            }
        }

    }

    public class OnlineStorageSection : ConfigurationSection
    {
        [ConfigurationProperty("Servers", IsRequired=false)]
        public ServerCollection Servers
        {
            get
            {
                ServerCollection servers = (ServerCollection)base["Servers"];
                return servers;
            }
        }

    }

    public class GuidanceExplorerSection : ConfigurationSection
    {
        [ConfigurationProperty("title", IsRequired = false)]
        public string Title
        {
            get
            {
                return (string)this["title"];
            }
            set
            {
                this["title"] = value;
            }
        }

        [ConfigurationProperty("feedbackLink", IsRequired = false)]
        public string FeedbackLink
        {
            get
            {
                return (string)this["feedbackLink"];
            }
            set
            {
                this["feedbackLink"] = value;
            }
        }
    }
}

